package demo7

import (
	"fmt"
	"strings"
)

// 逻辑入口
type CDDriver struct {
	Data string
}

func (c *CDDriver) ReadData() {
	c.Data = "music,image"
	fmt.Printf("CDDriver is read %s\n", c.Data)
	GetMediatorInstance().changed(c)
}

// 逻辑拆分
type CPU struct {
	Video string
	Sound string
}

func (c *CPU) Process(data string) {
	sp := strings.Split(data, ",")
	c.Video = sp[0]
	c.Sound = sp[1]

	fmt.Println(c.Video, c.Sound)
	GetMediatorInstance().changed(c)
}

// 业务拆分
type Video struct {
	Data string
}

func (v *Video) Display(data string) {
	v.Data = data
	fmt.Println("video", v.Data)
	GetMediatorInstance().changed(v)

}

type Sound struct {
	Data string
}

func (s *Sound) Play(data string) {
	s.Data = data
	fmt.Println("sound", s.Data)
	GetMediatorInstance().changed(s)

}

// 逻辑分配处理|上面是对该结构进行拆分
type mediator struct {
	cd  *CDDriver
	cpu *CPU
	s   *Sound
	v   *Video
}

var m *mediator

func GetMediatorInstance() *mediator {
	if m == nil {
		return &mediator{}
	}
	return m
}

func (m *mediator) changed(inst interface{}) {
	switch inst := inst.(type) {
	case *CDDriver:
		// 指针改变赋值 通过readData方法
		m.cpu.Process(inst.Data)
	case *CPU:
		m.s.Play(inst.Sound)
		m.v.Display(inst.Video)
	}
}
